import Store from '@/store'
import { changeLocale } from '@/i18n'
// 获取url上的国际化语言
export const getUrlLang = () => {
  const path = window.location.pathname
  let _lang = ''
  const Pattern = process.env.BASE_URL.split('/')[1] || ''
  const langPattern = Pattern ? new RegExp(`^\/${Pattern}\/([a-z\-]+)(\/|\b$)`, 'i') : new RegExp('^\/([a-z\-]+)(\/|\b$)', 'i')
  const execData = langPattern.exec(path)
  if (execData?.[1]) {
    const urlLang = execData?.[1]
    // if (localeList.includes(urlLang)) {
    //   _lang = urlLang
    // }
    _lang = urlLang
  }
  // console.log('getUrlLang:>>>>',_lang);
  return _lang
}
export const setDefaultLangAndTitle = (meta) => {
  const lang = getUrlLang()
  // 如果url带有语言则记录进vuex中
  if (lang) {
    Store.dispatch('locale/setLocalLang', lang)
    changeLocale(lang, meta)
  }
}
